package cloud.ben.smallscalepigfarmmanagementsystem.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;

/**
 * @Author: Ben
 * @Date: 2022/3/1 22:00
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class AuthUser implements UserDetails {
    /**
     * 表id
     */
    private Integer id;

    /**
     * 用户名
     */
    private String userName;

    /**
     * 密码
     */
    private String password;

    /**
     * 手机号
     */
    private String phoneNumber;

    /**
     * 邮箱
     */
    private String email;

    /**
     * 密保问题
     */
    private String  encryptedProblem;

    /**
     * 密保答案
     */
    private String  encryptedAnswer;

//    private Integer rememberMe;

    /**
     * 授权权限
     */
    private Collection<? extends GrantedAuthority> authorities;


    public AuthUser(String userName,String password) {
        this.userName = userName;
        this.password = password;
    }

    public AuthUser(User user) {
        this.id = user.getId();
        this.userName = user.getUserName();
        this.password = user.getPassword();
        this.email = user.getEmail();
        this.phoneNumber=user.getPhoneNumber();
        this.encryptedProblem=user.getEncryptedProblem();
        this.encryptedAnswer=user.getEncryptedAnswer();
        this.authorities = Collections.singleton(new SimpleGrantedAuthority(user.getRoles()));
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password ;
    }

    @Override
    public String getUsername() {
        return userName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

}
